package com.lcz.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class EncodingFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        request.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest,servletResponse);


        //1.防止用户未登录则进入
        String requestURI = request.getRequestURI();
        System.out.println("requestURI="+requestURI);
        Object username = request.getSession().getAttribute("username");

        if(requestURI.endsWith("index.jsp") && username==null){
            response.sendRedirect("login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
